﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DCT.Huiteng.Utility.Config;
using System.IO;
using DCT.Huiteng.Utility;
using DCT.Huiteng.Utility.Miscellaneous;

namespace DCT.Huiteng.AdminSystem.Web.Codes
{
    public class ToolBasePage : Page
    {        
        protected IConfigParser Config
        {
            get
            {
                return GeneralToolCode.LoadConfig("toolpages");
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            QueueExecution qe = GeneralToolCode.CreateQueueExecutionInstance("ToolBasePage");

            string falseStep = "";
            bool succ = qe.Run(new Func<QueueExecutionContext, bool>[] 
            { 
                this.CheckIP 
            }, out falseStep);

            if (!succ)
            {
                this.Response.Redirect("~/NoAuthorization.htm");
            }
            else
            {
                base.OnLoad(e);
            }
        }

        private bool CheckIP(QueueExecutionContext context)
        {
            bool valid = false;
            if (this.Request.IsLocal)
            {
                valid = true;
            }

            if (!valid)
            {
                string clientip = HttpExtUtility.GetClientIP();
                //查询配置
                valid = this.Config.GetListVal("validIps").Contains(clientip);
            }
            return valid;
        }
    }
}